<?php
/**
 * Pole pre model
 * @author Tomas Srna
 * @package bakalarka
 */
 
class Field
{
	/**
	 * @var string $name		Nazov pola
	 */
	public $name = '';
	/**
	 * @var string $datatype	Datovy typ
	 */
	public $datatype = '';
	/**
	 * @var bool $allownull	Povolit NULL
	 */
	public $allownull = false;
	/**
	 * @var mixed $default 		Predvolena hodnota
	 */
	public $default = NULL;
	/**
	 * @var bool $autoincrement	
	 */
	public $autoincrement = false;
	/**
	 * Primary key
	 */
	public $pk = false;
	
	/**
	 * Konstruktor
	 */
	public function __construct($name, $datatype, $pk = false,
		$allownull = false, $default = NULL, $autoincrement = false)
	{
		$this->name = $name;
		$this->datatype = $datatype;
		$this->pk = $pk;
		$this->allownull = $allownull;
		$this->default = $default;
		$this->autoincrement = $autoincrement;
	}
	
	/**
	 * Je primarny kluc?
	 * @return bool Je primarny kluc? 
	 */
	public function isPK()
	{
		return $this->pk;
	}
	
	/**
	 * Konverzia do stringu
	 */
	public function __toString()
	{
		return "`$this->name` $this->datatype".
				(!$this->allownull ? " NOT NULL" : ($this->default === NULL ? " DEFAULT NULL" : "")).
				($this->default !== NULL && $this->default != 'undefined' ?
					 " DEFAULT ".($this->default instanceof Expression ? 
					 				$this->default : "'".$this->default."'") : "").
				($this->autoincrement ? " AUTO_INCREMENT" : "");
				
	}
	
}
